﻿#region Using Directives

using System;
using System.Web.Caching;
using System.Web.Security;
using System.Collections.Generic;

using QuickDev.Common.Serialization;
using QuickDev.Common.Utils;

using QuickDev.Model;
using QuickDev.SQLServerDAL;
using QuickDev.Common.Caching.Manager;

#endregion

namespace QuickDev.Caching
{
    /// <summary>
    /// Cache Layer For dbo.SYS_Provider.
    /// </summary>
    public class ProviderCache
    {
        #region Constants

        private const string ProviderModelCacheKey = "_Provider_CacheKey_{0}";
        private const string ProviderCollectionCacheKey = "_Provider_Collection_{0}_{1}";

        #endregion

        #region Refresh Cache

        /// <summary>
        /// Refreshes the collection in the cache.
        /// This will make that the collection should reinsert itself on the next page request
        /// </summary>
        public static void RemoveCollectionFromCache( ProviderType providerType, CollectionsStatus status )
        {
            CacheHelper.RemoveCacheObject<string>( string.Format( ProviderCollectionCacheKey, providerType, status ) );
        }

        /// <summary>
        /// Refreshes the collection in the cache.
        /// This will make that the collection should reinsert itself on the next page request
        /// </summary>
        public static void RemoveAllCollectionFromCache()
        {
            CacheHelper.RemoveAllCacheObjectStartWith( ProviderCollectionCacheKey.Substring( 0, ProviderCollectionCacheKey.LastIndexOf( "_Collection_" ) + "_Collection_".Length ) );
        }

        /// <summary>
        /// Removes the Object Info from the cache.
        /// This will make that the Object should reinsert itself on the next page request
        /// </summary>
        /// <param name="key">The key.</param>
        public static void RemoveModelFromCache( int providerId )
        {
            CacheHelper.RemoveCacheObject<string>( string.Format( ProviderModelCacheKey, providerId ) );
        }

        #endregion

        #region ProviderCache.GetModel( int providerId ) Method

        /// <summary>
        /// public Provider GetModel( int providerId )
        /// </summary>
        /// <param name="providerId">providerId</param>
        /// <returns>Provider model</returns>
        public static Provider GetModel( int providerId )
        {
            Validation.ValidateIntegerIsGreaterThanZero( providerId, "QuickDev.Caching.ProviderCache.GetModel -- providerId " );

            return CacheHelper.CacheObject<Provider>
            (
                delegate
                {
                    return ProviderDAL.GetModel( providerId );
                },
                string.Format( ProviderModelCacheKey, providerId ),
                CacheLength.GetLongCacheTime,
                CacheItemPriority.High
            );
        }
        #endregion

        #region ProviderCache.GetList( ProviderType providerType , CollectionsStatus status ) Method

        /// <summary>
        /// public IList(Provider) GetList( ProviderType providerType , CollectionsStatus status )
        /// </summary>
        /// <param name="providerType">providerType</param>
        /// <param name="status">status</param>
        /// <returns>IList(Provider)</returns>
        public static List<Provider> GetList( ProviderType providerType, CollectionsStatus status )
        {
            return CacheHelper.CacheObject<List<Provider>>
            (
                delegate
                {
                    return ProviderDAL.GetList( (int)providerType, (int)status );
                },
                string.Format( ProviderCollectionCacheKey, providerType, status ),
                CacheLength.GetLongCacheTime,
                CacheItemPriority.High
            );
        }
        #endregion
    }
}

